package com.dd.cloud.user.controller;


import com.dd.cloud.user.entity.search.SearchHistory;
import com.dd.cloud.user.entity.search.SearchHot;
import com.dd.cloud.user.req.search.SearchHistoryReq;
import com.dd.cloud.user.res.chknowl.ChUserSearchListRes;
import com.dd.cloud.user.service.search.ISearchHistoryService;
import com.dd.cloud.user.service.search.ISearchHotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 热门搜索表 前端控制器
 * </p>
 *
 * @author czg
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ISearchHistoryService searchHistoryService;
    @Autowired
    private ISearchHotService searchHotService;

    /**
     * 全局搜索中医知识库
     *
     * @return 全局搜索中医知识库
     */
    @GetMapping("/global/chknow")
    public ResponseEntity<List<ChUserSearchListRes>> getCatalogueList(@NotNull(message = "搜索内容不能为空") String keyword) {
        List<ChUserSearchListRes> list = searchHistoryService.queryChByKeyword(keyword);
        return ResponseEntity.ok(list);
    }

    /**
     * 查询热门搜索
     *
     * @return 全局搜索中医知识库
     */
    @GetMapping("/hot/list")
    public ResponseEntity<List<SearchHot>> getHotList() {
        List<SearchHot> list = searchHotService.getSearchHotList();
        return ResponseEntity.ok(list);
    }

    /**
     * 添加热门搜索
     *
     * @return 全局搜索中医知识库
     */
    @PostMapping("/hot/add")
    public ResponseEntity<List<SearchHot>> hotAdd(String modular,
                                                  String submodule,
                                                  @NotBlank(message = "搜索内容不能为空") String search) {
        searchHotService.saveSearchHot(modular, submodule, search);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询历史搜索
     *
     * @return
     */
    @GetMapping("/history/list")
    public ResponseEntity<List<SearchHistory>> getHistoryList(String modular) {
        List<SearchHistory> list = searchHistoryService.getSearchHistory(modular);
        return ResponseEntity.ok(list);
    }

    /**
     * 清空历史搜索
     *
     * @return 全局搜索中医知识库
     */
    @PostMapping("/history/clean")
    public ResponseEntity<Void> historyClean(String modular) {
        searchHistoryService.cleanSearchHistory(modular);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加历史记录
     */
    @PostMapping("/history/record")
    public ResponseEntity<Void> addHistoryRecord(@Valid @RequestBody SearchHistoryReq req) {
        searchHistoryService.addSearchHistory(req.getKeyword(), req.getModular());
        return ResponseEntity.ok().build();
    }

    /**
     * 删除历史
     */
    @PostMapping("/remove/history")
    public ResponseEntity<Void> removeHistory(@NotNull(message = "id不能为空") Integer id) {
        searchHistoryService.removeHistory(id);
        return ResponseEntity.ok().build();
    }

}

